'use strict';

ionicTea.controller('teaHomeCtrl', function ($scope, $ionicModal,$state,$rootScope, Upload, $cordovaCamera, $cordovaImagePicker, $ionicPlatform, $ionicHistory, $ionicLoading, HttpService, UserService, $ionicActionSheet, HttpUploadService, $stateParams, HttpService2, ShareService) {

	$ionicModal.fromTemplateUrl('./templates/teaHome/city-list.html', {
        id: 'cityList',
        scope: $scope,
        animation: 'slide-in-right'
    }).then(function (modal) {
        $scope.cityListModal = modal;
    });

    $ionicModal.fromTemplateUrl('./templates/teaHome/talk-search.html', {
        id: 'search',
        scope: $scope,
        animation: 'slide-in-left'
    }).then(function (modal) {
        $scope.searchModal = modal;
    });

    $ionicModal.fromTemplateUrl('./templates/teaHome/talk-comment.html', {
        id: 'talk-comment',
        scope: $scope,
        animation: 'slide-in-right'
    }).then(function (modal) {
        $scope.talk_comment_modal = modal;
    });
    
    $ionicModal.fromTemplateUrl('./templates/teaHome/tea-shop.html', {
        id: 'tea-shop',
        scope: $scope,
        animation: 'slide-in-right'
    }).then(function (modal) {
        $scope.tea_shop_modal = modal;
    });

    $scope.show_search = function(){
    	$scope.searchModal.show();
    };

    $scope.cancel_search = function(){
    	$scope.searchModal.hide();
    	$scope.search_name = '';
    };

    $scope.openCityListModal = function () {
        $scope.cityListModal.show();
    };

    $scope.closeCityListModal = function () {
        $scope.cityListModal.hide();
    };

    $scope.currentCityChange = function (data) {
        $scope.closeCityListModal();
        $scope.gps_place = data;
        var param = {
        	city:data
        };
        $scope.initTeaShopList(param);

    };

	//切换按钮  功能 待封装
	$scope.teaTalk_bt = true;
	$scope.teaShop_bt = false;

	$scope.tea_shop = false;
	$scope.tea_talk = true;

	$scope.gps_place = '';
	$scope.isLast = true;

	/*加载茶谈*/
	$scope.teaTalk_list;
	$scope.goTeaTalk = function () {

		$scope.teaTalk_bt = true;
		$scope.teaShop_bt = false;
		$scope.tea_shop = false;
		$scope.tea_talk = true;

		$scope.gps_place = '';

		$scope.loadLastTalk();

		//加载广告
		$scope.getAdList();
	};
	
	$scope.currPage = 1;
	//最新发布茶谈
	$scope.loadLastTalk = function () {
		$scope.isLast = true;
		$scope.currPage = 1;
		$scope.isStop_scroll = true;

		//同步加载动画
		/*$ionicLoading.show({
			template: 'Loading...'
		});*/

		var callback = {
			success: function success(result) {

				console.log("茶谈加载完毕");
				console.log(result);
				$scope.isStop_scroll = false;
				//把图片处理成插件所需的格式
				var length = result.result.length;
				for(var i=0;i<length;i++){
					var imgs = result.result[i].imglist;
					//if(imgs.length == 0)continue;
					var items = [];
					for(var img in imgs){
						var item = {
							src:imgs[img],
							thumb:imgs[img]
						};
						items.push(item);
					}
					result.result[i].imglist = items;
				}
				$scope.teaTalk_list = result.result;

				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
			},
			failed: function failed(err) {
				console.log(err.statusText);
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
			}
		};

		var user = UserService.getUserAcceptCache();
		var param = {page: 1};
		if(user)param.viewer_id = user.id;
		
		HttpService({ name: 'blog/list',params:param,callback: callback });
	};

	//精选茶谈
	$scope.loadEliteTalk = function () {
		$scope.isLast = false;
		$scope.currPage = 1;
		$scope.isStop_scroll = true;

		var callback = {
			success: function success(result) {
				console.log("精选茶谈加载完毕");
				$scope.teaTalk_list = result.result;
				console.log($scope.teaTalk_list);
				$scope.isStop_scroll = false;
				//把图片处理成插件所需的格式
				var length = result.result.length;
				for(var i=0;i<length;i++){
					var imgs = result.result[i].imglist;
					//if(imgs.length == 0)continue;
					var items = [];
					for(var img in imgs){
						console.log("图片链接："+imgs[img]);
						var item = {
							src:imgs[img],
							thumb:imgs[img]
						};
						items.push(item);
					}
					console.log("格式转换后："+JSON.stringify(items));
					result.result[i].imglist = items;
				}
				$scope.teaTalk_list = result.result;
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
				$scope.$broadcast('scroll.infiniteScrollComplete');
			},
			failed: function failed(err) {
				console.log(err.statusText);
			}
		};

		var user = UserService.getUserAcceptCache();
		var param = {page:1};
		if(user)param.viewer_id = user.id;

		HttpService({ name: 'blog/elite',params: param, callback: callback });

	};
	
	//滚动加载茶谈
	$scope.isStop_scroll = true;
	$scope.scroll_load_talk = function(){
		$scope.currPage += 1;
		
		var callback = {
			success: function success(result) {

				console.log("茶谈加载完毕");
				console.log(result);
				//把图片处理成插件所需的格式
				var length = result.result.length;
				if(length == 0){
					$scope.isStop_scroll = true;
					return;
				}
				
				for(var i=0;i<length;i++){
					var imgs = result.result[i].imglist;
					//if(imgs.length == 0)continue;
					var items = [];
					for(var img in imgs){
						var item = {
							src:imgs[img],
							thumb:imgs[img]
						};
						items.push(item);
					}
					result.result[i].imglist = items;
					$scope.teaTalk_list.push(result.result[i]);
				}

				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
				$scope.$broadcast('scroll.infiniteScrollComplete');
			},
			failed: function failed(err) {
				console.log(err.statusText);
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}
		};

		var user = UserService.getUserAcceptCache();
		var param = {page: $scope.currPage};
		if(user)param.viewer_id = user.id;
		console.log("滚动 加载茶谈  记住是茶谈 参数："+JSON.stringify(param));
		
		HttpService({ name: 'blog/list',params:param,callback: callback });
	}
	
	$scope.scroll_load_edit = function(){
		$scope.currPage += 1;
		
		var callback = {
			success: function success(result) {
				console.log("精选茶谈加载完毕");
				console.log($scope.teaTalk_list);
				$scope.isStop_scroll = false;
				//把图片处理成插件所需的格式
				var length = result.result.length;
				if(length == 0){
					$scope.isStop_scroll = true;
					return;
				}
				for(var i=0;i<length;i++){
					var imgs = result.result[i].imglist;
					//if(imgs.length == 0)continue;
					var items = [];
					for(var img in imgs){
						console.log("图片链接："+imgs[img]);
						var item = {
							src:imgs[img],
							thumb:imgs[img]
						};
						items.push(item);
					}
					console.log("格式转换后："+JSON.stringify(items));
					result.result[i].imglist = items;
					$scope.teaTalk_list.push(result.result[i]);
				}
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
				$scope.$broadcast('scroll.infiniteScrollComplete');
			},
			failed: function failed(err) {
				console.log(err.statusText);
			}
		};

		var user = UserService.getUserAcceptCache();
		var param = {page:$scope.currPage};
		if(user)param.viewer_id = user.id;
		console.log("滚动 加载茶谈 参数："+JSON.stringify(param));

		HttpService({ name: 'blog/elite',params: param, callback: callback });
	}
	

	$scope.photoBrowserStandalone = function(index, images){
	    var myApp = new Framework7({
	        init: false, //IMPORTANT - just do it, will write about why it needs to false later
	    });
	    var myPhotoBrowserStandalone = myApp.photoBrowser({
	        type: 'standalone',
	        theme: 'light',
	        photos : images,
	        initialSlide: index,
	        onClose: function(){
	          myApp = undefined;
	        }
	    });
	    myPhotoBrowserStandalone.open();
	  }

	/*加载茶馆*/
	$scope.goTeaShop = function () {
		$scope.teaTalk_bt = false;
		$scope.teaShop_bt = true;
		$scope.tea_shop = true;
		$scope.tea_talk = false;

		var city = remote_ip_info['city'];
		$scope.gps_place = city;

		var param = {page:'1'};

		$scope.initTeaShopList(param);
	};

	$scope.shopList = [];

	$scope.initTeaShopList = function (param) {
		$scope.currPage = 1;
		$scope.isStop_scroll = true;
		var user = UserService.getUserAcceptCache();
		if (user) param.user_id = user.id;

		var callback = {
			success: function success(data) {
				// 显示地址列表
				$scope.shopList = data.result;
				$scope.isStop_scroll = false;
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
			}
		};
		HttpService({ name: 'teahouse/list', params: param, callback: callback });
	};
	
	//滚动加载茶馆
	$scope.scroll_load_shop = function(){
		$scope.currPage += 1;
		var param = {page:$scope.currPage};
		
		var user = UserService.getUserAcceptCache();
		if (user) param.user_id = user.id;

		var callback = {
			success: function success(data) {
				// 显示地址列表
				var length = data.result.length;
				if(length == 0){
					$scope.isStop_scroll = true;
					return;
				}
				for(var i=0;i<length;i++){
					$scope.shopList.push(data.result[i]);
				}
				//结束加载动画
				$ionicLoading.hide();
				$scope.$broadcast('scroll.refreshComplete');
			}
		};
		
		console.log();
		HttpService({ name: 'teahouse/list', params: param, callback: callback });
	}

	$scope.goToUserDetailPage = function (id) {
		UserService.goToUserDetailPage(id);
	};

	/*根据状态选择按钮触发事件*/
	$scope.chooseEvent = function () {
		if ($scope.teaTalk_bt) {
			$scope.addTeaTalk();
		}
		if ($scope.teaShop_bt) {
			var city = remote_ip_info['city'];
    		$scope.gps_place = city;
    		$scope.openCityListModal();
		}
	};

	/**增加茶谈*/
	$scope.addTeaTalk = function () {
		$state.go("add-talk");
	};
	$scope.add_talk_init = function () {
		$scope.teaTalk_info = { imglist: [] };
		$scope.imgSrcList = [];
	};

	/*进入茶馆**/
	$scope.curShop = {};
	$scope.goto_shop = function (item) {
		$scope.load_shop_info(item.id);
		$scope.curShop = item;
		$scope.tea_shop_modal.show();
		//$state.go("tea-shop", { id: item.id }, {});
	};
	//茶谈信息(添加是)
	$scope.teaTalk_info = { imglist: [] };
	$scope.imgSrcList = [];

	//选择相册或者拍照
	$scope.selectImgType = function () {

		// 显示操作表
		$ionicActionSheet.show({
			cssClass: "user-sheet",
			buttons: [{ text: '拍照' }, { text: '从相册选择' }],
			titleText: '选择图片',
			cancelText: '取消',
			buttonClicked: function buttonClicked(index) {
				if (index == 0) {
					$scope.takePhoto();
				}
				if (index == 1) {
					$scope.from_gallery();
				}
				return true;
			}
		});
	};

	//相册选择
	$scope.from_gallery = function () {

		// destinationType: Camera.DestinationType.FILE_URI,
		var options = {
      targetWidth: 1080, //照片宽度
			targetHeight: 1920, //照片高度
			sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
		};

		$cordovaCamera.getPicture(options).then(function (imageURI) {
			console.log('imageURI = ' + imageURI);
			$scope.teaTalk_info.imglist.push(imageURI);
			$scope.imgSrcList.push(imageURI);
		}, function (err) {
			// error
		});
	};

	//拍照
	$scope.takePhoto = function () {

		var options = {
			quality: 100, //相片质量0-100
			destinationType: Camera.DestinationType.FILE_URI, //返回类型：DATA_URL= 0，返回作为 base64 編碼字串。 FILE_URI=1，返回影像档的 URI。NATIVE_URI=2，返回图像本机URI (例如，資產庫)
			sourceType: Camera.PictureSourceType.CAMERA, //从哪里选择图片：PHOTOLIBRARY=0，相机拍照=1，SAVEDPHOTOALBUM=2。0和1其实都是本地图库
			allowEdit: false, //在选择之前允许修改截图
			encodingType: Camera.EncodingType.JPEG, //保存的图片格式： JPEG = 0, PNG = 1
			targetWidth: 1080, //照片宽度
			targetHeight: 1920, //照片高度
			mediaType: 0, //可选媒体类型：圖片=0，只允许选择图片將返回指定DestinationType的参数。 視頻格式=1，允许选择视频，最终返回 FILE_URI。ALLMEDIA= 2，允许所有媒体类型的选择。
			cameraDirection: 0, //枪后摄像头类型：Back= 0,Front-facing = 1
			popoverOptions: CameraPopoverOptions,
			saveToPhotoAlbum: true
		};

		$cordovaCamera.getPicture(options).then(function (imageURI) {
			console.log('imageURI = ' + imageURI);
			$scope.teaTalk_info.imglist.push(imageURI);

			$scope.imgSrcList.push(imageURI);
		}, function (err) {});
	};

	$scope.doRefresh = function () {
		
		if($scope.tea_talk){
			if($scope.isLast)
		    $scope.loadLastTalk();
		    else $scope.loadEliteTalk();
		}else{
			$scope.goTeaShop();
		}
		
	};

	$scope.sent = function () {
		var user = UserService.getUserAcceptCache();
		if (!user) return;

		var content = $scope.teaTalk_info.content;
		var imglist = $scope.teaTalk_info.imglist;

		var user_id = user.id;

		var talkCallback = {
	    	success: function success() {
                $rootScope.alert_suc("发布茶谈成功");
                $state.go('tab.teaHome')
	    	},
	    	failed: function failed(msg) {
                $rootScope.alert_err("发布茶谈失败："+msg);
	    	},
		};

		if (imglist == null || imglist.length == 0) {
			var params = {
				'content': content,
				'user_id': user_id,
			};

			console.log(JSON.stringify(params));

			HttpService({ name: 'blog/create', params: params, callback: talkCallback });
		} else {
			var callback = {
				success: function success(data) {

				console.log(JSON.stringify(data));
	//                var params = {
	//                    'imglist[0]': data[0],
	//                    'imglist[1]': data[1],
	//                    'imglist[2]': data[2],
	//                    'imglist[3]': data[3],
	//                    'imglist[4]': data[4],
	//                    'imglist[5]': data[5],
	//                    'imglist[6]': data[6],
	//                    'imglist[7]': data[7],
	//                    'imglist[8]': data[8],
	//                    'content': content,
	//                    'user_id': user_id,
	//                };

	                var params = {
	                    'imglist': data,
	                    'content': content,
	                    'user_id': user_id,
	                };

	                console.log(JSON.stringify(params));

	                HttpService({ name: 'blog/create', params: params, callback: talkCallback });

				}
			};


			HttpUploadService.uploadMuitl(imglist, callback);

		}



	};

	//跳转茶谈评论
	$scope.user_is_followed = false;
	$scope.is_from_talk = true;
	$scope.goto_talk_comntent = function (talk) {
		$scope.is_from_talk = true;
		$scope.load_talk_info(talk.id);
		$scope.user_is_followed = talk.user.is_followed;
		$scope.load_talk_comments(talk.id);
		$scope.curr_blog_id = talk.id;
		$scope.is_stop_load_talk = true;
		$scope.talk_com_currPage = 1;
		$scope.talk_comment_modal.show();
	};

	//关闭茶谈评论
	$scope.close_talk_comntent = function(){
		if($scope.is_from_talk){
			$scope.talk_comment_modal.hide();
		}else{
			$scope.back_from();
		}
		
	};

	//modal模式评论加载茶谈
	//茶谈品论分页
	$scope.curr_blog_id = '';
	$scope.is_stop_load_talk = true;
	$scope.talk_com_currPage = 1;
	
	//树初始化茶谈评论信息
	$scope.talk_info = {};
	$scope.talk_comment_list = { page: 1 };
	$scope.init_talk_comment = function () {
		$scope.is_from_talk = false;
		var blog_id = $stateParams.blog_id;
		$scope.user_is_followed = $stateParams.user_is_followed;
		$scope.load_talk_info(blog_id);
		$scope.curr_blog_id = blog_id;
		$scope.is_stop_load_talk = true;
		$scope.talk_com_currPage = 1;
		$scope.load_talk_comments(blog_id);
	};
	
	$scope.scroll_load_talk_comments = function(){
		
		$scope.talk_com_currPage += 1;
		
		var param = {
			blog_id: $scope.curr_blog_id,
			page: $scope.talk_com_currPage
		};
		
		console.log("滚动加载茶谈 评论参数："+JSON.stringify(param));
		
		//获取茶谈评论
		var callback = {
			success: function success(data) {
				console.log("茶谈评论列表:");console.log(data);
				var length = data.result.length;
				if(length == 0){
					$scope.is_stop_load_talk = true;
					return;
				}
				for(var i=0;i<length;i++){
					$scope.talk_comment_list.push(data.result[i]);
				}
				$scope.$broadcast('scroll.infiniteScrollComplete');
			}
		};

		HttpService({ name: 'blog/comment/list', params: param, callback: callback });
	}

	//加载茶谈明细
	$scope.talk_img_list = [];
	$scope.load_talk_info = function (blog_id) {
		//获取茶谈明细
		var callback = {
			success: function success(data) {
				console.log("茶谈明细:");console.log(data);
				$scope.talk_info = data;
				//把图片处理成插件所需的格式
				var imgs = data.imglist;
				var length = data.imglist.length;
				if(length == 0)return;
				var items = [];
				for(var i=0;i<length;i++){
					var item = {
						src:imgs[i],
						thumb:imgs[i]
					};
					items.push(item);
				}
				data.imglist = items;
				$scope.talk_img_list = items;
				//$scope.talk_info.imglist = items;
			}
		};

		HttpService({ name: 'blog/show', params: { blog_id: blog_id }, callback: callback });
	};
	//加载茶谈评论列表
	$scope.load_talk_comments = function (blog_id) {
		//获取茶谈明细
		var callback = {
			success: function success(data) {
				console.log("茶谈评论列表:");console.log(data);
				$scope.talk_comment_list = data.result;
				$scope.is_stop_load_talk = false;
			}
		};

		HttpService({ name: 'blog/comment/list', params: { blog_id: blog_id, page: $scope.talk_comment_list.page }, callback: callback });
	};

	//评论提交
	$scope.comment_content = {content:''};
	$scope.talk_comment_submit = function () {
		var user = UserService.getUserAcceptCache();
		if (!user) return;
		var param = {
			blog_id: $scope.talk_info.id,
			user_id: user.id,
			content: $scope.comment_content.content,
			imglist: []
		};
		
		var callback = {
			success: function success(data) {
				console.log("茶谈评论:");console.log(data);
				$scope.comment_content.content = '';
				$scope.is_stop_load_talk = true;
				$scope.talk_com_currPage = 1;
				$scope.load_talk_comments($scope.talk_info.id);
			}
		};

		HttpService({ name: 'blog/comment/create', params: param, callback: callback });
	};

	//加载广告
	$scope.ad = null;
	$scope.getAdList = function () {
		var callback = {
			success: function success(data) {
				console.log("加载广告");console.log(data)
				$scope.adList = data;
				$scope.ad = data[0]
			}
		};

		HttpService({ name: 'advert/list', params: {}, callback: callback });
	};

	//跳转广告
	$scope.goToAdPage = function (id) {
		$state.go('ad-detail', {id: id});
	};

	//茶谈转发
	// $scope.tea_talk_share = function (talkItem) {
	// 	$state.go("share-talk");
	// };

	$scope.share = function () {
		ShareService.shareImgMsg($scope.teaHouseData.name, $scope.teaHouseData.imglist);
	};

	$scope.tea_talk_share = function (talkItem) {
	    var shareImgArray = [];
	    var pos = 0;
	    var size = talkItem.imglist.length;

	    if (size != null && size>0) {
	        for(;pos < size; pos++) {
                shareImgArray.push(talkItem.imglist[pos].src);
            }
            ShareService.shareImgMsg(talkItem.content, shareImgArray);
	    } else {
	        ShareService.share(talkItem.content);
	    }
	};

	//关注用户
	$scope.follow_user = function (follower_id, isFollow) {
		var user = UserService.getUserAcceptCache();
		if (!user) return;

		var param = {
			user_id: follower_id,
			follower_id: user.id
		};

		// var param = {
		// 	user_id: user.id,
		// 	follower_id: follower_id,
		// };

		console.log("关注用户："+JSON.stringify(param));

		var callback = {
			success: function success(data) {
				console.log("关注茶谈用户返回:");console.log(data);
				if (isFollow) {
					$rootScope.alert_suc("已取消关注");
					$scope.user_is_followed = false;
				} else {
					$rootScope.alert_suc("关注成功");
					$scope.user_is_followed = true;
				}

				$scope.loadLastTalk();

			}
		};

		if (isFollow) {
			HttpService({ name: 'user/follow/cancel', params: param, callback: callback });
		} else {
			HttpService({ name: 'user/follow', params: param, callback: callback });
		}

	};

	/*茶谈点赞*/
	$scope.talk_upvote = function (blog) {
		var user = UserService.getUserAcceptCache();
		if (!user) return;

		var param = {
			user_id:user.id,
			blog_id:blog.id
		};

		console.log("茶谈点赞："+JSON.stringify(param));

		var callback = {
			success: function success(data) {
				console.log("茶谈点赞返回:");console.log(data);
				//$scope.loadLastTalk();
				if(data.code == 200){
					blog.is_upvote = true;
					blog.up_count += 1;
				}
			}
		};
		HttpService({ name: 'blog/upvote', params: param, callback: callback });
	}
	
	//取消茶谈点赞
	$scope.talk_upvote_cancel = function (blog) {
		var user = UserService.getUserAcceptCache();
		if (!user) return;

		var param = {
			user_id:user.id,
			blog_id:blog.id
		};

		console.log("茶谈取消点赞："+JSON.stringify(param));

		var callback = {
			success: function success(data) {
				console.log("茶谈取消点赞返回:");console.log(data);
				//$scope.loadLastTalk();
				if(data.code == 200){
					blog.is_upvote = false;
					blog.up_count -= 1;
				}
			}
		};
		HttpService({ name: 'blog/upvote/cancel', params: param, callback: callback });
	}

	/*键盘搜索*/
	$scope.search_name = '';
	$scope.search_list = [];
	$scope.key_search = function(e){
		var keycode = window.event?e.keyCode:e.which;
		var name = e.target.value;
        if(keycode==13){
            $scope.search_product(name);
            e.target.value = '';
        }
	};

	/*搜索产品*/
	$scope.search_product = function(name){
		var param = {
			name: name,
			page: '1'
		};

		console.log("搜索参数："+JSON.stringify(param));
		var callback = {
			success: function success(data) {
				console.log("搜索商品返回:");console.log(data);
				$scope.search_name = '';
				$scope.search_list = data.result;
			}
		};

		HttpService({ name: 'product/search', params: param, callback: callback });
	};

	//跳转商品详情
	$scope.goto_product_info = function (id) {
		$scope.cancel_search();
		$state.go('product-info', { product_id: id });
	};

	/*--------------茶馆-----------------*/
	$scope.tea_house_follow = function (item) {
		var user = UserService.getUserAcceptCache(true);
		if (!user) return;

		var param = {
			teahouse_id: item.id,
			user_id: user.id
		};

		var callback = {
			success: function success(data) {
				if (item.is_follow) {
					item.is_follow = false;
					$rootScope.alert_suc("已取消关注");
				} else {
					item.is_follow = true;
					$rootScope.alert_suc("已关注");
				}

			}
		};

		if (item.is_follow) {
			HttpService({ name: 'teahouse/follow/cancel', params: param, callback: callback });
		} else {
			HttpService({ name: 'teahouse/follow', params: param, callback: callback });
		}

	};
	
	//加载茶馆明细
	$scope.teaHouseData = {};
	$scope.isFollow = '';
	$scope.load_shop_info = function (id) {
		var user = UserService.getUserAcceptCache(false);
		if (!user) return;
		
		var callback = {
			success: function success(data) {
				$scope.teaHouseData = data;
				console.log("茶馆明细：");console.log(data);
				if (data.is_follow) {
					$scope.isFollow = '已收藏';
				}else{
					$scope.isFollow = '收藏';
				}
			},
			failed: function failed(err) {}
		};
	
		HttpService({ name: 'teahouse/show', params: {
				user_id: user.id,
				teahouse_id: id
			}, callback: callback });
	}
	
	$scope.tea_shop_close = function () {
		$scope.tea_shop_modal.hide();
	};
	
	$scope.tea_shop_collect = function () {
		var user = UserService.getUserAcceptCache(true);
		if (!user) return;
		
		var callback = {
			success: function success(data) {
				$scope.teaHouseData.is_follow = !$scope.teaHouseData.is_follow;
				if ($scope.teaHouseData.is_follow) {
					$scope.isFollow = '已收藏';
					$scope.curShop.is_follow = true;
				} else {
					$scope.isFollow = '收藏';
					$scope.curShop.is_follow = false;
				}
			}
		};

		var url = $scope.teaHouseData.is_follow ? 'teahouse/follow/cancel' : 'teahouse/follow';

		HttpService({ name: url, params: {
				user_id: user.id,
				teahouse_id: $scope.teaHouseData.id
			}, callback: callback });
	};

	$scope.tea_shop_share = function () {
		ShareService.shareImgMsg($scope.teaHouseData.name, $scope.teaHouseData.imglist);
	};
	
	$scope.back_from = function () {
		$rootScope.back();
	};

	$scope.back = function () {
		$state.go($state.previous.name, $state.previous.params);
	};
});

ionicTea.controller('teaHomeDetailCtrl', function ($scope,$rootScope, $state, $stateParams, $ionicHistory, $ionicPopup, HttpService, ShareService, UserService) {

	var teaHouseID = $stateParams.id;

	var user = UserService.getUserAcceptCache();

	$scope.teaHouseData = {
		name: '',
		content: '',
		phone: '',
		address: '',
		imglist: [],
		image: ''
	};

	$scope.isFollow = '收藏';

	var callback = {
		success: function success(data) {
			$scope.teaHouseData = data;
			console.log("收藏茶馆：");console.log(data);
			if (data.is_follow) {
				$scope.isFollow = '已收藏';
			}
		},
		failed: function failed(err) {}
	};

	HttpService({ name: 'teahouse/show', params: {
			user_id: user.id,
			teahouse_id: teaHouseID
		}, callback: callback });

	$scope.collect = function () {

		var callback = {
			success: function success(data) {
				$scope.teaHouseData.is_follow = !$scope.teaHouseData.is_follow;
				if ($scope.teaHouseData.is_follow) {
					$scope.isFollow = '已收藏';
				} else {
					$scope.isFollow = '收藏';
				}
			}
		};

		var url = $scope.teaHouseData.is_follow ? 'teahouse/follow/cancel' : 'teahouse/follow';

		HttpService({ name: url, params: {
				user_id: user.id,
				teahouse_id: teaHouseID
			}, callback: callback });
	};

	$scope.call_store = function(tel){
		if(tel){
			$window.location.href="tel:"+tel;
		}
	}

	$scope.back_from = function () {
	   $rootScope.back();
	};

	$scope.share = function () {
		ShareService.shareImgMsg($scope.teaHouseData.name, $scope.teaHouseData.imglist);
	};
});
